local blaze  = require "blaze"
local app = require "app"
local game = require "club.threecard.sudoku"

local M = game.ui.activity()

M.store_watchers = {
}

function M:ctor()
    self.res = "club.threecard.sudoku/ui/main.b:Feedback"

    local Toggle = require "club.threecard.sudoku.ui.common.Toggle"
    self.misc_toggle = Toggle.new(true)
    self.rule_toggle = Toggle.new(false)
    self.exception_toggle = Toggle.new(false)
end

function M:on_injected(bind)
    bind("gameobject.element", self.misc_toggle_obj, self.misc_toggle)
    bind("gameobject.element", self.rule_toggle_obj, self.rule_toggle)
    bind("gameobject.element", self.exception_toggle_obj, self.exception_toggle)
    bind("button.click", self.submit_btn, self.on_submit)
end

function M:on_set_parent()
    self:super("on_set_parent")
    self.transform:Stretch()
end

function M:on_submit()
    if #self.content_input.text == 0 then
        app.toast.show(game.i18n.text("feedback:content placeholder"))
        return
    end
    local url = string.format("%s/v1/feedback", app.appconfig.entry_server)
    local avatar_state = app.store.avatar.state
    blaze.http.post(url, {
        userId = avatar_state.user_id or 0,
        userName = avatar_state.name or "",
        type = "0",
        contact = "",
        content = self.content_input.text,
    }, {
        type = "json",
        immediate = true
    })
    local args = 
    {
        content = app.i18n.text("feedback:thank you")
    }
    app.msgbox.show(args)
end

return M